<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Config;

class Route extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index(){
        $data=input();
        if($this::$isdelete){
            $map['r.isdelete']=1;
        }else{
            $map['r.isdelete']=0;
        }
        if($data['title']){
            $map['r.title']=array('like',"%{$data['title']}%");
        }
        if($data['pass']){
            $map['r.pass']=array('like',"%{$data['pass']}%");
        }
        if($data['tag']){
            $map['r.tag']=array('like',"%{$data['tag']}%");
        }
        if($data['mfw_sku_id']){
            $map['mfw_sku_id']=array('like',"{$data['mfw_sku_id']}");
        }
        $listRows = $this->request->param('numPerPage') ? : Config::get("paginate.list_rows");
        $list = DB::name('route')
            ->alias('r')
            ->join('tp_route_category rc','r.category_id=rc.id','left')
            ->field('r.*,rc.name')
            ->where($map)
            ->order('id','desc')
            ->paginate($listRows, false, ['query' => $this->request->get()]);
            $this->view->assign('list',$list);
            $this->view->assign("count", $list->total());
            $this->view->assign("page", $list->render());
            $this->view->assign('numPerPage', $list->listRows());
            return $this->view->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit(){
        $data = input();
        $id = $data['id'];
//        var_dump($data);
        if($this->request->isAjax()){
            $data['go_time']=strtotime($data['go_time']);
            $data['end_time']=strtotime($data['end_time']);
            //dump($data);
            unset($data['id']);
            if($data['main_img']){
                $data['main_img']=$data['main_img'][0];
            }
            if ($data['day'] && !is_numeric($data['day'])){
                return ajax_return_adv_error('全程天数不符合格式');
            }elseif (!$data['day']){
                return ajax_return_adv_error('全程天数不能为空');
            }
            $rs = Db::name('route')->where('id',$id)->update($data);
            Db::name("route_product")->where("route_id",$id)->delete();
            foreach ($data['product_id'] as $key=>$vle){
                $insert['route_id']=$id;
                $insert['route_category_id']=$data['route_category_id'][$key];
                $insert['product_id']=$data['product_id'][$key];
//                var_dump($insert);
                Db::name('route_product')->insert($insert);
//                var_dump(Db::name('route_product')->getLastSql());
            }
            if($rs!==false){
                return ajax_return_adv("修改成功");
            }else{
                return ajax_return_error("修改失败");
            }
        }else{
            $route  = Db::table('tp_route')
                ->alias('r')
                ->where('r.id',$id)
                ->field('r.*')
                ->find();
            $route_products=Db::name("route_product")
                ->alias("rp")
                ->join("tp_route_category rc","rc.id=rp.route_category_id","left")
                ->where('rp.route_id',$id)
                ->field("rp.*,rc.name as route_category_name")
                ->select();
            //全程天数 计算从1到100
            $day = [];
            for ($i=1; $i < 101; $i++){
                $day[] = $i;
            }
            $route['content'] =  htmlspecialchars_decode($route['content']);

            $category_list = Db::name('route_category')
                ->where('isdelete',0)
                ->select();
            $route['route_products']=$route_products;
            $this->view->assign('day',$day);
            $this->view->assign('vo',$route);
            $this->view->assign('category_list',$category_list);
            return $this->view->fetch();
        }
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        $data = input();
        $id = $data['id'];

        if($this->request->isAjax()){
            //dump($data);
            unset($data['id']);
            $data['create_time']=NOW_TIME;
            if($data['main_img']){
                $data['main_img']=$data['main_img'][0];
            }
            if ($data['day'] && !is_numeric($data['day'])){
                return ajax_return_adv_error('全程天数不符合格式');
            }elseif (!$data['day']){
                return ajax_return_adv_error('全程天数不能为空');
            }
            $data['go_time']=strtotime($data['go_time']);
            $data['end_time']=strtotime($data['end_time']);
            $id = Db::name('route')->strict(false)->insertGetId($data);
            Db::name("route_product")->where("route_id",$id)->delete();
            foreach ($data['product_id'] as $key=>$vle){
                $insert['route_id']=$id;
                $insert['route_category_id']=$data['route_category_id'][$key];
                $insert['product_id']=$data['product_id'][$key];
//                var_dump($insert);
                Db::name('route_product')->insert($insert);
//                var_dump(Db::name('route_product')->getLastSql());
            }
            if($id!==false){
                return ajax_return_adv("添加成功");
            }else{
                return ajax_return_error("添加失败");
            }
        }else{
            // 计算从1到100
            $day = [];
            for ($i=1; $i < 101; $i++){
                $day[] = $i;
            }
            $category_list = Db::name('route_category')
                ->where('isdelete',0)
                ->select();
            $this->view->assign('day',$day);
            $this->view->assign('category_list',$category_list);

            return $this->view->fetch("edit");
        }
    }
}
